home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / filesyst / dosfs / dmsdosfs.000 / dmsdosfs / dmsdosfs-0.6.9b / daemon_actions.c < prev    next >
C/C++ Source or Header  |  1996-06-30  |  4KB  |  195 lines

  1. /* 
  2.  
  3. daemon_actions.c
  4.  
  5. DMSDOS filesystem: dmsdos daemon
  6.  
  7. The DMSDOS filesystem was written by Frank Gockel
  8. (gockel@etecs4.uni-duisburg.de).
  9.  
  10. COPYRIGHT (C) 1995,1996 Frank Gockel
  11.  
  12. Distributed under the GNU General Public License.
  13.  
  14. */
  15.  
  16. #include<stdio.h>
  17. #include<linux/dmsdos_fs.h>
  18. #include<sys/ioctl.h>
  19. #include<sys/types.h>
  20. #include<sys/stat.h>
  21. #include<fcntl.h>
  22. #include<string.h>
  23. #include<errno.h>
  24. #include<signal.h>
  25. #include<unistd.h>
  26.  
  27. /* default compression level minus 1 for dmsdosd */
  28. int cfaktor=11;
  29.  
  30. int debug=0;
  31.  
  32. int exit_signaled=0;
  33.  
  34. #define CONTINUE 0
  35. #define EXIT_AND_FINISH 1
  36. #define EXIT_IMMEDIATELY 2
  37.  
  38. typedef struct
  39. { long val1;
  40.   long val2;
  41.   long val3;
  42.   unsigned char data[32*1024];
  43. } Cdata;
  44.  
  45. Cdata cdata;
  46. Cdata ckdata;
  47.  
  48. void signal_handler(int a)
  49. { if(a==SIGINT)exit_signaled=EXIT_AND_FINISH;
  50.   if(a==SIGTERM)exit_signaled=EXIT_IMMEDIATELY;
  51. }
  52.  
  53. int get_and_compress_one(int fd)
  54. { int ret;
  55.   int handle;
  56.   int length;
  57.   int size;
  58.   int method;
  59.   
  60.   /* get cluster to compress */
  61.   if(exit_signaled!=CONTINUE)return 2;
  62.   if(debug)printf("dmsdosd: Trying to read...\n");
  63.   ret=ioctl(fd,DMSDOS_D_READ,&cdata);
  64.   if(ret!=1)
  65.   { if(debug)printf("dmsdosd: nothing there - D_READ ioctl returned %d\n",ret);
  66.     return ret;
  67.   }
  68.   
  69.   handle=cdata.val1;
  70.   length=cdata.val2;
  71.   size=(length-1)/512+1;
  72.   method=cdata.val3;
  73.   if(debug)printf("dmsdosd: compressing...\n");
  74.   ret=compress(ckdata.data,cdata.data,size,method,0);
  75.   if(debug)printf("dmsdosd: compress returned %d\n",ret);
  76.   if(ret<0)ret=0; /* compression failed */
  77.   ckdata.val1=handle;
  78.   ckdata.val2=ret;
  79.   if(debug)printf("dmsdosd: writing...\n");
  80.   ioctl(fd,DMSDOS_D_WRITE,&ckdata);
  81.   
  82.   return 1; /* one cluster compressed */
  83. }
  84.  
  85.  
  86. void do_dmsdosd_actions(int fd)
  87. {
  88.   /* register dmsdosd */
  89.   if(debug)printf("dmsdosd: calling D_ASK...\n");
  90.   if(ioctl(fd,DMSDOS_D_ASK,NULL))
  91.   { printf("dmsdosd: can't get permissions. Try again as root.\n");
  92.     return;
  93.   }
  94.   
  95.   signal(SIGINT,signal_handler);
  96.   signal(SIGTERM,signal_handler);
  97.  
  98.   do
  99.   { while(get_and_compress_one(fd)==1);
  100.     /* don't kill the system performance when nothing to compress */
  101.     if(exit_signaled==CONTINUE)
  102.     { if(debug)printf("dmsdosd: sleeping...\n"); 
  103.       sleep(30); /* 30secs is the bdflush default */
  104.     }
  105.   }
  106.   while(exit_signaled==CONTINUE);  
  107.   
  108.   if(debug)printf("dmsdosd: calling D_EXIT...\n");
  109.   ioctl(fd,DMSDOS_D_EXIT,NULL);
  110.   
  111.   if(exit_signaled==EXIT_AND_FINISH)
  112.   { exit_signaled=CONTINUE;
  113.     while(get_and_compress_one(fd)==1);
  114.   }
  115.   
  116. }
  117.  
  118. void panic(char*text)
  119. { printf("dmsdosd panic: %s\n",text);
  120.   abort();
  121. }
  122.  
  123. int scan(char*arg)
  124. { int w;
  125.  
  126.   if(strncmp(arg,"0x",2)==0)sscanf(arg+2,"%x",&w);
  127.   else sscanf(arg,"%d",&w);
  128.   
  129.   return w;
  130. }
  131.  
  132. void error(int err)
  133. { if(err==-EPERM)printf("Permission denied.\n");
  134.   else if(err==-EIO)printf("I/O Error.\n");
  135.   else if(err==-EINVAL)printf("Invalid parameter.\n");
  136.   else printf("Error %d occured.\n",err);
  137. }
  138.  
  139. void main(int argc, char*argv[])
  140. { Dblsb dblsb;
  141.   int fd;
  142.   int ret;
  143.   
  144.   if(argc<2||argc>4)
  145.   {
  146.     printf("DMSDOS daemon (C) 1996 Frank Gockel\n");
  147.     printf("Usage: %s (directory)\n",argv[0]);
  148.     printf("       %s (directory) cf\n",argv[0]);
  149.     printf("       %s (directory) cf debug\n",argv[0]);
  150.     return;
  151.   }
  152.   
  153.   if(argc==4)debug=1;
  154.   
  155.   fd=open(argv[1],O_RDONLY);
  156.   if(fd<0)
  157.   { printf("No such file or directory.\n");
  158.     return;
  159.   }
  160.   
  161.   if(argc==3)
  162.   { cfaktor=scan(argv[2])-1;
  163.     if(cfaktor<0||cfaktor>11)
  164.     { printf("parameter out of range\n");
  165.       close(fd);
  166.       return;
  167.     }
  168.   }
  169.   
  170.   /* this hack enables reverse version check */
  171.   dblsb.s_dcluster=DMSDOS_VERSION;
  172.   
  173.   ret=ioctl(fd,DMSDOS_GET_DBLSB,&dblsb);
  174.   if(ret<0)
  175.   { printf("This is not a DMSDOS directory.\n");
  176.     close(fd);
  177.     return;
  178.   }
  179.   if(debug)printf("You are running DMSDOS Version %d.%d.%d.\n\n",(ret&0xff0000)>>16,
  180.          (ret&0x00ff00)>>8,ret&0xff);
  181.   if(ret&0x0f000000)
  182.   { printf("Sorry, this program is too old for the actual dmsdos version.\n");
  183.     close(fd);
  184.     return;
  185.   }
  186.   if(ret<0x00000607)
  187.   { printf("This program requires at least DMSDOS Version 0.6.7.\n");
  188.     close(fd);
  189.     return;
  190.   }
  191.  
  192.   do_dmsdosd_actions(fd);
  193.   
  194.   close(fd);
  195. }